﻿#include <iostream>
#include <assert.h>
#include <map>
#include <string>
#include "threadpool.h"

void Func1(int i, const std::string& msg)
{
	std::cout << i << "-->" << msg<< std::endl;
}

int Func2(int i, const std::string& msg)
{
	std::cout << i << "-->" << msg << std::endl;
	std::this_thread::sleep_for(std::chrono::milliseconds(1000));
	return i * 100;
}

class PrintTest {
public:
	void Func1(int i, const std::string& msg)
	{
		std::cout << i << "-->" << msg << std::endl;
	}

	void Func2(int i)
	{
		std::cout << i << "-->" << "测试成员函数" << std::endl;
	}
};

int main()
{
    CThreadPool pool;
	pool.Start(2);
	//测试lambda表达式
	pool.Commit([] {std::cout << "测试lambda表达式" << std::endl; });
	//测试带参数的lambda表达式
	pool.Commit([](int val){std::cout << "测试带参数的lambda表达式" << "-->" << val << std::endl; }, 999);
	//测试全局函数
	pool.Commit(Func1, 100, "测试全局函数");
	//测试成员函数
	PrintTest p;
	pool.Commit(std::mem_fn(&PrintTest::Func1),&p , 200, "测试成员函数");

	//测试同步获取结果
	auto res = pool.Commit(Func2, 300, "测试同步获取结果");
	auto val = res.get();
	std::cout << "获取到结果:" << val << std::endl;

	std::this_thread::sleep_for(std::chrono::milliseconds(5000));

    return 0;
}